package com.bijiawang.search.service;

import com.bijiawang.search.dao.PipelineDao;
import com.bijiawang.search.pojo.Goods;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.*;
import us.codecraft.webmagic.processor.PageProcessor;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class SearchOtherService  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    RedisTemplate redisTemplate;

    @Async
    public Future<ArrayList<Goods>> SearchTmall(String key) throws IOException {
        ValueOperations<String,ArrayList<Goods>> valueOperations = redisTemplate.opsForValue();
        ArrayList<Goods> List = valueOperations.get("Tmall" + key);
        if (List != null){
            return new AsyncResult<ArrayList<Goods>>(List);
        }else {
            String newKey = key.replace(" ", "");
            HttpGet httpGet = new HttpGet("https://list.tmall.com/search_product.htm?q=" + newKey + "&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&xl=3_1&from=mallfp..pc_1_suggest");
            logger.info("Downloading {}",httpGet.getURI());
            CloseableHttpClient build = HttpClients.custom().build();
            CloseableHttpResponse execute = build.execute(httpGet);
            HttpEntity entity = execute.getEntity();
            String content = EntityUtils.toString(entity, "utf-8");
            Document parse = Jsoup.parse(content);

            Elements productPrice = parse.getElementsByClass("productPrice");//价格

            Elements ShopName = parse.getElementsByClass("productShop-name");//商店名称

            Elements productTitle = parse.getElementsByClass("productTitle");//标题

            productTitle.get(1).getElementsByTag("a").attr("href");//商品链接

            Elements elementsByClass = parse.getElementsByClass("productImg-wrap");//图片链接
            ArrayList<Goods> goodsArrayList = new ArrayList<>();
            for (int i = 0;i < productPrice.size();i++){
                Goods goods = new Goods();
                goods.setPrice(productPrice.get(i).text());
                goods.setShopName(ShopName.get(i).text());
                goods.setGoodsName(productTitle.get(i).text());
                goods.setGoodsUri(productTitle.get(i).getElementsByTag("a").attr("href"));
                String a = elementsByClass.get(i).getElementsByTag("img").attr("data-ks-lazyload");
                if (a.equals("")){
                    a = elementsByClass.get(i).getElementsByTag("img").attr("src");
                }
                goods.setImageUri(a);
                goods.setPlatform("天猫");
                goodsArrayList.add(goods);
            }
            build.close();
            execute.close();
            logger.info("Success downloading {}",httpGet.getURI());
            valueOperations.set("Tmall" + key,goodsArrayList,43, TimeUnit.MINUTES);
            return new AsyncResult<ArrayList<Goods>>(goodsArrayList);
        }
    }

    class JD implements PageProcessor{

        private Site site = Site.me()
                .setCharset("utf8")
                .setTimeOut(5000)
                .setRetrySleepTime(2000) //重试的间隔时间
                .setSleepTime(3); //重试的次数
        @Override
        public void process(Page page) {
            ArrayList<Goods> goodsArrayList = new ArrayList<>();
            List<String> sku_List = page.getHtml().css("div#J_goodsList > ul > li", "data-sku").all();
            for (String sku : sku_List) {
                Goods goods = new Goods();
                //获取商品详情地址
                String itemUrl = "https://item.jd.com/"+ sku + ".html";
                goods.setGoodsUri(itemUrl);
                //获取商品图片地址
                String picurl = page.getHtml().xpath("//div[@id=J_goodsList]/ul/li[@data-sku="+sku+"]/div/div[@class=p-img]/a/img/@src").toString();
                goods.setImageUri(picurl);
                //获取商品价格
                String price = Jsoup.parse(page.getHtml().xpath("//div[@id=J_goodsList]/ul/li[@data-sku="+sku+"]/div/div[@class=p-price]/strong").toString()).text();
                goods.setPrice(price);
                //获取商品标题
                String title = Jsoup.parse(page.getHtml().xpath("//div[@id=J_goodsList]/ul/li[@data-sku="+sku+"]/div/div[@class=p-name]/a/em").toString()).text();
                goods.setGoodsName(title);
                String shopName = Jsoup.parse(page.getHtml().xpath("//div[@id=J_goodsList]/ul/li[@data-sku="+sku+"]/div/div[@class=p-shop]").toString()).text();
                goods.setShopName(shopName);
                goods.setPlatform("京东");
                goodsArrayList.add(goods);
                page.putField("List", goodsArrayList);
            }
        }

        @Override
        public Site getSite() {
            return site;
        }
    }

    @Async
    public Future<ArrayList<Goods>> SearchJD(String key){
        ValueOperations<String,ArrayList<Goods>> valueOperations = redisTemplate.opsForValue();
        ArrayList<Goods> List = valueOperations.get("JD" + key);
        if (List != null){
            return new AsyncResult<ArrayList<Goods>>(List);
        }else {
            String newKey = key.replace(" ", "");
            PipelineDao pipelineDao = new PipelineDao();
            Spider.create(new JD())
                    .addUrl("https://search.jd.com/Search?keyword=" + newKey + "&enc=utf-8&wq=%E6%AF%9B%E5%B7%BE&pvid=d37d71e320b145ca8425084a40ccb2d9") //设置爬取的页面
                    .thread(4)
                    .addPipeline(pipelineDao)
                    .run();
            ArrayList<Goods> goodsArrayList = pipelineDao.getList();
            valueOperations.set("JD" + key,goodsArrayList,46, TimeUnit.MINUTES);
            return new AsyncResult<ArrayList<Goods>>(goodsArrayList);
        }
    }




    @Async
    public Future<ArrayList<Goods>> SearchDD(String key) throws IOException {
        ValueOperations<String,ArrayList<Goods>> valueOperations = redisTemplate.opsForValue();
        ArrayList<Goods> List = valueOperations.get("DD" + key);
        if (List != null){
            return new AsyncResult<ArrayList<Goods>>(List);
        }else {
            String newKey = key.replace(" ", "");
            HttpGet httpGet = new HttpGet("http://search.dangdang.com/?key=" + newKey + "&act=input&show=big&show_shop=0#J_tab");
            logger.info("Downloading {}",httpGet.getURI());
            CloseableHttpClient build = HttpClients.custom().build();
            CloseableHttpResponse execute = build.execute(httpGet);
            HttpEntity entity = execute.getEntity();
            String content = EntityUtils.toString(entity, "GB2312");
            Document parse = Jsoup.parse(content);

            Elements bigimg_cloth_shoplist = parse.getElementsByClass("bigimg cloth_shoplist");
            Elements li = bigimg_cloth_shoplist.get(0).getElementsByTag("li");//ALL
            ArrayList<Goods> goodsArrayList = new ArrayList<>();
            for (Element now : li) {
                Goods goods = new Goods();
                Elements name = now.getElementsByClass("name");
                goods.setGoodsName(name.text());//标题
                Elements pic = now.getElementsByClass("pic");
                String attr = pic.get(0).getElementsByTag("img").attr("data-original");
                if (attr.equals("")){
                    attr = pic.get(0).getElementsByTag("img").attr("src");
                }
                goods.setImageUri(attr);//图片链接
                goods.setGoodsUri(pic.attr("href"));//商品链接
                String price = now.getElementsByClass("price_n").text();
                if (price.substring(0, 1).equals("¥")) {
                    goods.setPrice(price);//价格
                } else {
                    continue;
                }
                String link = now.getElementsByClass("link").text();
                if (link.equals("")) {
                    link = "当当自营";
                }
                goods.setShopName(link);//店铺名称
                goods.setPlatform("当当");
                goodsArrayList.add(goods);
            }
            build.close();
            execute.close();
            logger.info("Success downloading {}",httpGet.getURI());
            valueOperations.set("DD" + key,goodsArrayList,40, TimeUnit.MINUTES);
            return new AsyncResult<ArrayList<Goods>>(goodsArrayList);
        }
    }

    public String SearchDDOne(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);

        CloseableHttpClient build = HttpClients.custom().build();
        CloseableHttpResponse execute = build.execute(httpGet);
        HttpEntity entity = execute.getEntity();
        String content = EntityUtils.toString(entity, "GB2312");
        Document parse = Jsoup.parse(content);
        Elements id = parse.getElementsByClass("J_change_price");
        return id.text();
    }

    public String SearchTmallOne(String key) throws IOException {
        String newKey = key.replace(" ", "");
        HttpGet httpGet = new HttpGet("https://list.tmall.com/search_product.htm?q=" + newKey + "&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&xl=3_1&from=mallfp..pc_1_suggest");
        CloseableHttpClient build = HttpClients.custom().build();
        CloseableHttpResponse execute = build.execute(httpGet);
        HttpEntity entity = execute.getEntity();
        String content = EntityUtils.toString(entity, "utf-8");
        Document parse = Jsoup.parse(content);

        Elements productPrice = parse.getElementsByClass("productPrice");//价格
        String text = productPrice.get(0).text();
        build.close();
        execute.close();
        return text.substring(1);
    }

    class JDOne implements PageProcessor{

        private Site site = Site.me()
                .setCharset("utf8")
                .setTimeOut(5000)
                .setRetrySleepTime(2000) //重试的间隔时间
                .setSleepTime(3); //重试的次数
        @Override
        public void process(Page page) {
            List<String> sku_List = page.getHtml().css("div#J_goodsList > ul > li", "data-sku").all();
            String sku = sku_List.get(0);
            //获取商品价格
            String price = Jsoup.parse(page.getHtml().xpath("//div[@id=J_goodsList]/ul/li[@data-sku="+sku+"]/div/div[@class=p-price]/strong").toString()).text();
            String substring = price.substring(1);
            page.putField("price", substring);
        }

        @Override
        public Site getSite() {
            return site;
        }
    }

    public String SearchJDOne(String key){
        String newKey = key.replace(" ", "");
        PipelineDao pipelineDao = new PipelineDao();
        Spider.create(new JDOne())
                .addUrl("https://search.jd.com/Search?keyword=" + newKey + "&enc=utf-8&wq=%E6%AF%9B%E5%B7%BE&pvid=d37d71e320b145ca8425084a40ccb2d9") //设置爬取的页面
                .thread(4)
                .addPipeline(pipelineDao)
                .run();
        return pipelineDao.getPrice();

    }
}